import { defineStore } from 'pinia'
import router from '@/router/index'
import useSysStore from './sysStore'
const useUserStore = defineStore('userStore', {
  state: () => ({ 
    _userInfo:JSON.parse(localStorage.getItem('userInfo') as string||"{}") || {},
    _token:localStorage.getItem('token')|| "",
    _MenuTreeList:JSON.parse(localStorage.getItem('MenuTreeList') as string||"[]") || [],
  }),
  getters: {
     userInfo():UserType.UserInfo{
      return this._userInfo
    },
    token():string{
      return this._token 
    },
    MenuTreeList():Array<UserType.MenuInfo>{
      return this._MenuTreeList
    }
  },
  actions: {
    setUserInfo(userInfo:UserType.UserInfo){
      this._userInfo = userInfo
      localStorage.setItem('userInfo',JSON.stringify(userInfo))
    },
    setToken(token:string){
      this._token = token

      localStorage.setItem('token',token) 
    },
    setUserMenuData(MenuTreeList:Array<UserType.MenuInfo>){
      localStorage.setItem('MenuTreeList',JSON.stringify(MenuTreeList))
      this._MenuTreeList = MenuTreeList
    },
    logout(){
      let sysStore = useSysStore()
      this._userInfo = {}
      this._token = ""
      this._MenuTreeList = []
      localStorage.removeItem('userInfo')
      localStorage.removeItem('token')
      localStorage.removeItem('MenuTreeList')
      sysStore.removeMenuConfig()
      router.replace('/login')
    }
      
  },
})

export default useUserStore